package com.xtzn.business.impl;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.xtzn.business.IIosSystemVersionBusiness;
import com.xtzn.click.vo.CSResponse;
import com.xtzn.common.Constant;
import com.xtzn.core.enums.ResponseEnum;
import com.xtzn.mapper.entity.IosSystemVersion;
import com.xtzn.service.IIosSystemVersionService;
import com.xtzn.vo.CSRequest;


/**
 * 功能描述: <br>
 * **
 * 
 * @author zyw
 * @version [版本号, 2018年4月14日]
 */
@Component
public class IosSystemVersionBusinessImpl implements IIosSystemVersionBusiness {

	@Autowired
	private IIosSystemVersionService iosSystemVersionService;
	/**
	 * 功能描述: <br>
	 * **
	 *
	 * @author zyw
	 * @version [版本号, 2018年4月14日]
	 */
	@Override
	public CSResponse retrieveIosSystemVersionById(Integer id) {
		IosSystemVersion iosSystemVersion = iosSystemVersionService.selectIosSystemVersionById(id);
		if(null!=iosSystemVersion){
			return new CSResponse(ResponseEnum.SUCCESS.getCode(),ResponseEnum.SUCCESS.getDesc(),(Serializable)iosSystemVersion);
		}
		return new CSResponse(ResponseEnum.DATA_ERROR.getCode(),ResponseEnum.DATA_ERROR.getDesc());
	}

	/**
	 * 功能描述: <br>
	 * **
	 *
	 * @author zyw
	 * @version [版本号, 2018年4月14日]
	 */
	@Override
	public CSResponse retrieveIosSystemVersionByPage(CSRequest csRequest) {
		Integer pageSize = Constant.pageSize;
		Integer skips = (csRequest.getPageIndex()-1)*pageSize;
		List<IosSystemVersion> list = iosSystemVersionService.selectIosSystemVersionByPage(csRequest.getUserId(), skips, pageSize);
		if(!list.isEmpty()){
			Map<String,Object> map = new HashMap<String, Object>();	
			Integer count  = iosSystemVersionService.selectCount(csRequest.getUserId());
			map.put("data", list);
			map.put("count", count);
			return new CSResponse(ResponseEnum.SUCCESS.getCode(),ResponseEnum.SUCCESS.getDesc(),(Serializable)map);
		}
		return new CSResponse(ResponseEnum.DATA_ERROR.getCode(),ResponseEnum.DATA_ERROR.getDesc());
	}

	/**
	 * 功能描述: <br>
	 * **
	 *
	 * @author zyw
	 * @version [版本号, 2018年4月14日]
	 */
	@Override
	public CSResponse createIosSystemVersion(IosSystemVersion iosSystemVersion) {
		int i = iosSystemVersionService.insertIosSystemVersion(iosSystemVersion);
		if(i>0){
			return new CSResponse(ResponseEnum.SUCCESS.getCode(),ResponseEnum.SUCCESS.getDesc());
		}
		return new CSResponse(ResponseEnum.DATA_ERROR.getCode(),ResponseEnum.DATA_ERROR.getDesc());
	}

	/**
	 * 功能描述: <br>
	 * **
	 *
	 * @author zyw
	 * @version [版本号, 2018年4月14日]
	 */
	@Override
	public CSResponse updateIosSystemVersion(IosSystemVersion iosSystemVersion) {
		int i = iosSystemVersionService.updateIosSystemVersion(iosSystemVersion);
		if(i>0){
			return new CSResponse(ResponseEnum.SUCCESS.getCode(),ResponseEnum.SUCCESS.getDesc());
		}
		return new CSResponse(ResponseEnum.DATA_ERROR.getCode(),ResponseEnum.DATA_ERROR.getDesc());
	}

	/**
	 * 功能描述: <br>
	 * **
	 *
	 * @author zyw
	 * @version [版本号, 2018年4月14日]
	 */
	@Override
	public CSResponse deleteById(Integer id) {
		int i = iosSystemVersionService.deleteById(id);
		if(i>0){
			return new CSResponse(ResponseEnum.SUCCESS.getCode(),ResponseEnum.SUCCESS.getDesc());
		}
		return new CSResponse(ResponseEnum.DATA_ERROR.getCode(),ResponseEnum.DATA_ERROR.getDesc());
	}

	/**
	 * 功能描述: <br>
	 * **
	 *
	 * @author zyw
	 * @version [版本号, 2018年4月14日]
	 */
	@Override
	public CSResponse deleteBatchById(List<Integer> list) {
		int i = iosSystemVersionService.deleteBatchById(list);
		if(i>0){
			return new CSResponse(ResponseEnum.SUCCESS.getCode(),ResponseEnum.SUCCESS.getDesc());
		}
		return new CSResponse(ResponseEnum.DATA_ERROR.getCode(),ResponseEnum.DATA_ERROR.getDesc());
	}

	/**
	 * 功能描述: <br>
	 * **
	 *
	 * @author zyw
	 * @version [版本号, 2018年4月14日]
	 */
	@Override
	public CSResponse retrieveIosSystemVersionByGroup(IosSystemVersion iosSystemVersion) {
		List<Map<String,Object>> list = iosSystemVersionService.selectIosSystemVersionByGroup(iosSystemVersion.getUserId());
		if(!list.isEmpty()){
			for(int i = 0;i<list.size();i++){
				String versionNumber = (String)list.get(i).get("versionNumber");
				String[] versionNumbers = versionNumber.split(",");
				list.get(i).put("versionNumber", versionNumbers);
			}	
			return new CSResponse(ResponseEnum.SUCCESS.getCode(),ResponseEnum.SUCCESS.getDesc(),(Serializable)list);
		}
		return new CSResponse(ResponseEnum.DATA_ERROR.getCode(),ResponseEnum.DATA_ERROR.getDesc());
	}

}
